package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.ActivitiesDto;
import com.heima.dto.ActivitiesPageDto;
import com.heima.pojo.Activities;
import com.heima.result.PageResult;
import com.heima.result.Result;
import com.heima.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/clues/activity")
@Api(tags = "活动管理")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 新增活动
     * @return
     */
    @ApiOperation("新增活动")
    @PostMapping
    @OperationFill
    public Result insertActivity(@RequestBody ActivitiesDto activitiesDto){
        log.info("新增活动：{}",activitiesDto);
        activityService.insertActivity(activitiesDto);
        return Result.success();
    }

    /**
     * 活动查询
     * @param activitiesPageDto
     * @return
     */
    @ApiOperation("活动查询")
    @GetMapping("/list")
    @OperationFill
    public PageResult queryActivitiesList(ActivitiesPageDto activitiesPageDto,
                                          @RequestParam(value = "params[beginTime]",required = false) String begin,
                                          @RequestParam(value = "params[endTime]",required = false) String end,
                                          @RequestParam(value = "params[beginCreateTime]",required = false) String beginCreate,
                                          @RequestParam(value = "params[endCreateTime]",required = false) String endCreate
    ){
        log.info("活动查询：{}",activitiesPageDto);
        activitiesPageDto.setBeginTime(begin);
        activitiesPageDto.setEndTime(end);
        activitiesPageDto.setBeginCreateTime(beginCreate);
        activitiesPageDto.setEndCreateTime(endCreate);
        log.info("活动查询：{}",activitiesPageDto);
        PageResult pageResult = activityService.queryActivitiesList(activitiesPageDto);
        return pageResult;
    }

    /**
     * 修改界面信息回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @OperationFill
    public Result getActivitiesInfo(@PathVariable Integer id){
        log.info("修改活动：{}",id);
        Activities activities = activityService.getActivitiesInfo(id);
        return Result.success(activities);
    }

    /**
     * 修改活动
     * @param activities
     * @return
     */
    @PutMapping
    @OperationFill
    public Result updateActivities(@RequestBody Activities activities){
        log.info("修改活动：{}",activities);
        activityService.updateActivities(activities);
        return Result.success();
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @OperationFill
    public Result deleteActivities(@PathVariable Integer id){
        log.info("删除活动：{}",id);
        activityService.deleteActivities(id);
        return Result.success();
    }
}
